package telnetserver;

public class Menu {
    private Telnet telnet;
    private String skin;
    private int x;
    private Object[] buttonLinks;
    private String[] buttons;
    
    public Menu(Telnet t) {
        telnet = t;
    }
    
    public void setMenu(Object[] newButtonLinks) {
        buttonLinks = newButtonLinks;
        buttons = new String[buttonLinks.length];
        for (int i = 0; i < buttonLinks.length; i++) {
            buttons[i] = buttonLinks[i].getClass().getSimpleName().toUpperCase();
        }
        
        int[] offset = {0, 1};
        
        int max = 0;
        for (String s : buttons) {
            if (s.length() > max) max = s.length();
        }
        
        max += 4;
        
        x = 40 - (max / 2);
        
        int[] startxy = {x + 1, 7};
        
        skin = telnet.skin(1, 27, 1, "___  __            __  ___");
        skin += telnet.skin(27, 2, " |  | _| |   |\\ | | _|  |");
        skin += telnet.skin(27, 3, " |  |__  |__ | \\| |__   |");
        
        for (int i = 0; i < (max - 4) / 2; i++) {
            skin += telnet.skin(x + i, 5, "_");
        }
        
        skin += telnet.skin(x + (max / 2) - 2, 5, "MENU");
        
        for (int i = 0; i < (max - 4) / 2; i++) {
            skin += telnet.skin(x + ((max - 4) / 2) + 4 + i, 5, "_");
        }
        
        x--;
        
        skin += telnet.skin(x, 6, "|");
        skin += telnet.skin(x + max, 6, "|");
        
        for (int i = 0; i < buttons.length; i++) {
            skin += telnet.skin(x, 7 + i,"|  ");
            skin += telnet.skin(x + max, 7 + i, "|");
        }
        
        skin += telnet.skin(x, 7 + buttons.length, "|");
        skin += telnet.skin(x + max, 7 + buttons.length, "|");
        
        for (int i = 0; i < max - 1; i++) {
            skin += telnet.skin(x + 1 + i, 7 + buttons.length, "_");
        }
    }
    
    public void addUser(User u) {
        u.setCanWrite(false);
        u.setCanPress(true);
        
        u.clear();
        u.prepare(skin);
        u.setButtons(buttons, null);
        u.fixButtonsXY(x + 2, 7, 1);
        u.drawButtons();
    }
    
    public void read(User u, String s) {
        telnet.changeMode(u, buttonLinks[u.getButton()[1]]);
    }
}